package cn.bigway.app.weavingdesigner;

import java.io.UnsupportedEncodingException;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

import javax.swing.Icon;
import javax.swing.JLabel;
import javax.swing.UIManager;

import cn.bigway.app.weavingdesigner.log.WdLog;
import cn.bigway.app.weavingdesigner.type.WdMenuItemType;

public class Resources {
	private static final String BUNDLE_NAME = "Messages";

	private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle
			.getBundle(BUNDLE_NAME);

	public enum LabelType {
		HIGTH

	}

	private Resources() {
		//
	}

	public static String getString(String key) {
		try {
			try {
				return new String(RESOURCE_BUNDLE.getString(key).getBytes(
						"ISO-8859-1"), "UTF8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		} catch (MissingResourceException e) {
			WdLog.error("can't find message string! key=" + key);
		}
		return '!' + key + '!';
	}

	public static <E extends Enum<E>> String getString(E type, String key) {
		return getString(type.name().toLowerCase() + "." + key);
	}

	public static <E extends Enum<E>> Integer getInteger(E type, String key) {
		try {
			return Integer.valueOf(getString(type, key));
		} catch (Exception e) {
			return null;
		}
	}

	public static JLabel getLabel(LabelType type) {
		return new JLabel(getString("label." + type.name().toLowerCase()));
	}

	public static Icon getIcon(WdMenuItemType type) {
		Icon icon = UIManager.getIcon(Resources.getString(type, "icon"));
		return (null == icon) ? UIManager.getIcon("OptionPane.informationIcon")
				: icon;
	}
}
